﻿
using System;
using System.Web.UI;
using umbraco.cms.presentation.Trees;
using umbraco.editorControls;
using Umbraco.Core.IO;

namespace Auros.Categorisation.Umbraco
{
    [ValidationProperty("Value")]
    public class CategoryPicker : BaseTreePickerEditor
    {
        string valueByConstructor = null;

        public CategoryPicker() : base() 
        { 
        
        }

        public CategoryPicker(umbraco.interfaces.IData data) : base(data) 
        { 

        }

        public CategoryPicker(string value) : base() 
        {
            this.valueByConstructor = value;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!String.IsNullOrEmpty(this.valueByConstructor))
            {
                this.Value = this.valueByConstructor;
                this.ItemTitle.InnerHtml = this.GetItemTitle();
            }
        }

        public override string TreePickerUrl
        {
            get
            {
                return TreeService.GetPickerUrl("categories", "categories");
            }
        }

        public override string ModalWindowTitle
        {
            get
            {
                return "Select a category";
            }
        }

        protected override string GetItemTitle()
        {
            int id = -1;

            if (Int32.TryParse(this.Value, out id))
            {
                var category = Categories.GetCategory(id);

                return category != null ? category.LocalizedName : String.Empty;
            }
            else
                return String.Empty;
        }

        protected override string GetJSScript()
        {
            /* 0 = this control's client id
             * 1 = label 
             * 2 = itemIdValueClientID
             * 3 = itemTitleClientID
             * 4 = itemPickerUrl
             * 5 = popup width
             * 6 = popup height
             * 7 = show header
             * 8 = umbraco path
            */
            return string.Format(@"
                var mc_{0} = new AurosCategorisationCategoryPicker('{0}','{1}','{2}','{3}','{4}',{5},{6},{7},'{8}');",
                    new string[]
                 {
                    this.ClientID,
                    ModalWindowTitle,
                    ItemIdValue.ClientID,                    
                    ItemTitle.ClientID,
                    TreePickerUrl,
                    ModalWidth.ToString(),
                    ModalHeight.ToString(),
                    ShowHeader.ToString().ToLower(),
                    IOHelper.ResolveUrl(SystemDirectories.Umbraco).TrimEnd('/')
                 });
        }

        protected override void RenderJSComponents()
        {
            if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                ScriptManager.RegisterClientScriptResource(this, typeof(CategoryPicker), "Auros.Categorisation.Scripts.CategoryPicker.js");
            else
                Page.ClientScript.RegisterClientScriptResource(typeof(CategoryPicker), "Auros.Categorisation.Scripts.CategoryPicker.js");
        }
    }
}